package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.Add2ListActivity;
import com.warmlight.voicepacket.MypalyModalCancel;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.RecommendFeed;
import com.warmlight.voicepacket.data.TempData;
import com.warmlight.voicepacket.data.eventbus.PlayerServiceData;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDetailDaoHelp;
import com.warmlight.voicepacket.fragment.CopyShareFragment;
import com.warmlight.voicepacket.ui.Add2NetListActivity;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.SPUtils;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_ID;

/**
 * 网络播单详情列表adapter
 */
public class PlayDetalAdapter extends MBaseAdapter {
    private  FragmentManager fragment;
    private  Boolean isBodan;
    private String type;
    private boolean isLogin;
    private String pushtitle;
    public PlayDetalAdapter(Context context, List<PlaydeatalData.DataBean.ListBean> list ,
                            FragmentManager fragmentManager,Boolean isbodan,String type,String pushtitle) {
        super(context, list);
        fragment=fragmentManager;
        isBodan=isbodan;
        this.type = type;
        this.pushtitle= pushtitle;
        if (SPUtils.isLogined()){
            isLogin = true;
        }else{
            isLogin = false;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final PlayDetalAdapter.ViewHolder holder;
        if (convertView != null) {
            holder = (PlayDetalAdapter.ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(mContext, R.layout.item_playdetail, null);
            holder = new PlayDetalAdapter.ViewHolder();
            holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
//            holder.iv_gengduo = (ImageView) convertView.findViewById(R.id.iv_gengduo);
            holder.iv_righticon = (ImageView) convertView.findViewById(R.id.iv_righticon);
            holder.iv_rightshoucang = (ImageView) convertView.findViewById(R.id.iv_rightshoucang);
            holder.iv_ischeck = (ImageView) convertView.findViewById(R.id.iv_ischeck);
            convertView.setTag(holder);
        }


        final PlaydeatalData.DataBean.ListBean listBean = (PlaydeatalData.DataBean.ListBean) (mList.get(position));
        final int isSelect = listBean.getIsSelect();
        if (isSelect == 0){
            holder.iv_ischeck.setVisibility(View.GONE);
            holder.tv_num.setVisibility(View.VISIBLE);
            holder.iv_rightshoucang.setVisibility(View.VISIBLE);
            holder.iv_righticon.setVisibility(View.VISIBLE);
        }else{
            holder.iv_ischeck.setVisibility(View.VISIBLE);
            holder.tv_num.setVisibility(View.GONE);
            holder.iv_rightshoucang.setVisibility(View.GONE);
            holder.iv_righticon.setVisibility(View.GONE);
            if (isSelect==1){
                holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_no);
            }else{
                holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_yes);
            }

        }

        if(listBean.isCheak()){//如果是播放 。。。做个判断
            holder.tv_num.setBackgroundResource(R.drawable.icon_playing_new);
            holder.tv_num.setText("");
            holder.tv_title.setTextColor(Color.parseColor("#CE3838"));
//            holder.tv_time.setText(((PlaydeatalData.DataBean.ListBean)(mList.get(position))).getDuration());
        }else {
            if (listBean.isPause()){
                holder.tv_num.setBackgroundResource(R.drawable.icon_pause_new);
                holder.tv_num.setText("");
                holder.tv_title.setTextColor(Color.parseColor("#CE3838"));
            }else {
                holder.tv_num.setBackgroundResource(R.color.color_fff);
                holder.tv_title.setTextColor(Color.parseColor("#333333"));
                holder.tv_num.setText(position + 1 + "");
            }
//            holder.tv_time.setText("");
        }
        if (!isBodan){//这里判断是否是本地播放页面
            holder.iv_righticon.setImageResource(R.drawable.icon_playshare_deep);
            if (isLogin){

                boolean is_collect = listBean.isIs_collect();
                if (is_collect) {
                    holder.iv_rightshoucang.setImageResource(R.drawable.icon_fav_y);
                } else {
                    holder.iv_rightshoucang.setImageResource(R.drawable.icon_paly_shoucang);
                }

            }else {
                boolean exists = DBFloatPlayListDetailDaoHelp.isExists(mContext, listBean.getId());
                if (exists) {
                    holder.iv_rightshoucang.setImageResource(R.drawable.icon_fav_y);
                } else {
                    holder.iv_rightshoucang.setImageResource(R.drawable.icon_paly_shoucang);
                }
            }

        } else {
            holder.iv_righticon.setVisibility(View.GONE);
        }
        holder.iv_righticon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d( "onClick: 'jinblai","为何进不来");
                new CopyShareFragment().show(fragment, "Dialog");//展示分享弹窗
                TempData.AudioData = changeBean((PlaydeatalData.DataBean.ListBean) mList.get(position));

                if (TextUtils.equals("push",type)){
                    //网络播单
                    MobclickAgent.onEvent(mContext,MobClickHelper.p_BAG_FEED_SHARE,
                            ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle()+"-"+pushtitle+"-"+ SPUtils.getLoginUid());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单分享", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                    ZhugeSDK.getInstance().track(mContext, MobClickHelper.p_BAG_FEED_SHARE, temp);

                    //
                    ClickLog.clickLog(ClickLog.p_bag_feed_share, listBean.getCover_id(), listBean.getId());
                }else if (TextUtils.equals("search",type)){
                    //网络播单
                    MobclickAgent.onEvent(mContext, MobClickHelper.s_BAG_FEED_SHARE, ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单分享", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                    ZhugeSDK.getInstance().track(mContext, MobClickHelper.s_BAG_FEED_SHARE, temp);

                    //
                    ClickLog.clickLog(ClickLog.s_bag_feed_share, listBean.getCover_id(), listBean.getId());
                }else {
                    //网络播单
                    MobclickAgent.onEvent(mContext, MobClickHelper.BAG_FEED_SHARE, ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单分享", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                    ZhugeSDK.getInstance().track(mContext, MobClickHelper.BAG_FEED_SHARE, temp);

                    //
                    ClickLog.clickLog(ClickLog.bag_feed_share, listBean.getCover_id(), listBean.getId());
                }
            }
        });
        holder.tv_title.setText(((PlaydeatalData.DataBean.ListBean)(mList.get(position))).getTitle());
        holder.iv_rightshoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    if (!isBodan){//收藏功能
                        //TODO 转换bean
                        RecommendFeed.DataBean.ListBean bean= new RecommendFeed.DataBean.ListBean();
                        PlaydeatalData.DataBean.ListBean mbean =(PlaydeatalData.DataBean.ListBean)(mList.get(position));

                        bean.setDuration(mbean.getDuration());
                        bean.setCategory_id(mbean.getCategory_id());
                        bean.setId(mbean.getId());
                        bean.setCollect(mbean.getCollect());
                        bean.setShare(mbean.getShare());
                        bean.setStar(mbean.getStar());
                        bean.setTitle(mbean.getTitle());
                        bean.setUrl(mbean.getUrl());
                        bean.setCover_pic(mbean.getPic());
                        bean.setCover_id(mbean.getCover_id());
                        bean.setCover_name(mbean.getCover_name());
                        if (SPUtils.isLogined()){
                            Intent addIntent = new Intent(mContext, Add2NetListActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(AUDIO_ID, bean);
                            addIntent.putExtras(bundle);
                            mContext.startActivity(addIntent);
                        }else {
                            Intent addIntent = new Intent(mContext, Add2ListActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(AUDIO_ID, bean);
                            addIntent.putExtras(bundle);
                            mContext.startActivity(addIntent);
                        }

                        if (TextUtils.equals("push",type)){
                            //网络播单
                            MobclickAgent.onEvent(mContext,MobClickHelper.p_BAG_FEED_CLOT,
                                    ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle()+"-"+pushtitle+"-"+ SPUtils.getLoginUid());
                            HashMap<String, Object> temp = new HashMap<>();
                            temp.put("网络播单收藏", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                            ZhugeSDK.getInstance().track(mContext, MobClickHelper.p_BAG_FEED_CLOT, temp);
//
                            ClickLog.clickLog( ClickLog.p_bag_feed_clot, listBean.getCover_id(), listBean.getId());
                        }else if (TextUtils.equals("search",type)){
                            //网络播单
                            MobclickAgent.onEvent(mContext,MobClickHelper.s_BAG_FEED_CLOT, ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                            HashMap<String, Object> temp = new HashMap<>();
                            temp.put("网络播单收藏", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                            ZhugeSDK.getInstance().track(mContext, MobClickHelper.s_BAG_FEED_CLOT, temp);
//
                            ClickLog.clickLog( ClickLog.s_bag_feed_clot, listBean.getCover_id(), listBean.getId());
                        }else {
                            //网络播单
                            MobclickAgent.onEvent(mContext, MobClickHelper.BAG_FEED_CLOT, ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                            HashMap<String, Object> temp = new HashMap<>();
                            temp.put("网络播单收藏", ((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                            ZhugeSDK.getInstance().track(mContext, MobClickHelper.BAG_FEED_CLOT, temp);
//
                            ClickLog.clickLog(ClickLog.bag_feed_clot, listBean.getCover_id(), listBean.getId());
                        }
                    }else {
                        TempData.AudioData = changeBean((PlaydeatalData.DataBean.ListBean) mList.get(position));
                        Intent intent = new Intent(mContext, MypalyModalCancel.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("mypaly",(PlaydeatalData.DataBean.ListBean)mList.get(position));
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);

                        //网络播单
                        MobclickAgent.onEvent(mContext, MobClickHelper.BAG_FEED_CLOT,((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                        HashMap<String,Object> temp = new HashMap<>();
                        temp.put("网络播单收藏",((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                        ZhugeSDK.getInstance().track(mContext,MobClickHelper.BAG_FEED_CLOT,temp);
                    }
            }
        });
//        Log.d("getView: ",mList.toString());
        return convertView;
    }


    public void startManager(){
        //暂停所有的音乐
        EventBus.getDefault().post(new PlayerServiceData(PlayerServiceData.STOP, "stop"));
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(1);
            o.setCheak(false);
            o.setPause(false);
        }
        notifyDataSetChanged();
    }

    public void selectAll(){
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(2);
        }
        notifyDataSetChanged();
    }

    public void remove(int arg0) {//删除指定位置的item
        mList.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    public void insert(PlaydeatalData.DataBean.ListBean item, int arg0) {//在指定位置插入item
        mList.add(arg0, item);
        this.notifyDataSetChanged();
    }

    public void cancelManager() {
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(0);
        }
        notifyDataSetChanged();
    }

    public void cancelSelectAll() {
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(1);
        }
        notifyDataSetChanged();
    }

    public void selectItem(int position) {
        PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(position);
        int isSelect = o.getIsSelect();
        if (isSelect==1){
//            holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_yes);
            o.setIsSelect(2);
        }else{
//            holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_no);
            o.setIsSelect(1);
        }
        notifyDataSetChanged();
    }

    public ArrayList<String> getSelect2() {
        ArrayList<String> select = new ArrayList<>();
        for (int i=0;i<getCount();i++) {
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            int isSelect = o.getIsSelect();
            if (isSelect == 2) {
                select.add(o.getId());
            }
        }
        return select;
    }

    public ArrayList<PlaydeatalData.DataBean.ListBean> getSelect(){
        ArrayList<PlaydeatalData.DataBean.ListBean> select = new ArrayList<>();
        for (int i=0;i<getCount();i++) {
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            int isSelect = o.getIsSelect();
            if (isSelect == 2) {
                select.add(o);
            }
        }
        return select;
    }

    public void notifyCollect() {
        notifyDataSetChanged();
    }

    public void notifyCollectById(String id) {
        for (int j =0;j<mList.size();j++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(j);
            if (TextUtils.equals(id,o.getId())){
                o.setIs_collect(true);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void notifyCollectByIds(String json) {
        ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(json);
        for (int i=0;i<listMapByJson.size();i++){
            String id = listMapByJson.get(i).get("id");
            for (int j =0;j<mList.size();j++){
                PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(j);
                if (TextUtils.equals(id,o.getId())){
                    o.setIs_collect(true);
                    break;
                }
            }
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView tv_num;
        TextView tv_title;
        TextView tv_time;
//        ImageView iv_gengduo;
        ImageView iv_righticon;
        ImageView iv_rightshoucang;
        ImageView iv_ischeck;
    }

    private RecommendFeed.DataBean.ListBean changeBean(PlaydeatalData.DataBean.ListBean temp){
        RecommendFeed.DataBean.ListBean bean = new RecommendFeed.DataBean.ListBean();
        bean.setTitle(temp.getTitle());
        bean.setId(temp.getId());
        bean.setUrl(temp.getUrl());
        bean.setDuration(temp.getDuration());
        bean.setStar(temp.getStar());
        bean.setShare(temp.getShare());
        bean.setCollect(temp.getCollect());
        bean.setCover_id(temp.getCover_id());
        return bean;
    }

}
